var fontnik = require('.');
var fs = require('fs');
var path = require('path');
var arguments = process.argv.splice(2);

var convert = function(fileName, outputDir) {
    var font = fs.readFileSync(path.resolve(fileName));
    output2pbf(font, 0, 255, outputDir);
}
 
function output2pbf(font, start, end, outputDir) {
    if (start > 65535) {
        console.log("done!");
        return;
    }
    fontnik.range({font: font, start: start, end: end}, function(err, res) {
        var outputFilePath = path.resolve(outputDir + start + "-" + end + ".pbf");
        fs.writeFile(outputFilePath, res, function(err){
            if(err) {
                console.error(err);
            } else {
                output2pbf(font, end+1, end+1+255, outputDir);
            }
        });
    });
}
 
// 下面是需要转换的字体，和转换后pbf存放路径
//convert("./fonts/open-sans/OpenSans-Regular.ttf", "./lgl/open-sans/");
if(arguments.length == 2)
{
	var outp = process.cwd()+"/"+arguments[1];
	fs.mkdir(outp,{recursive:true},(err)=>{
	    if(err){
	        throw err;
	    }else{
	        //console.log('ok!');
	    }
	});
	convert(process.cwd()+"/"+arguments[0], outp + "/");
}
